﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Collections;
namespace GPCSC.Web
{
    /// <summary>
    /// fileuploads 的摘要说明
    /// </summary>
    public class fileuploads : IHttpHandler
    {


        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";
            HttpPostedFile file = context.Request.Files["Filedata"];
            string uploadPath =
                HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";

            if (file != null)
            {
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                string type = file.FileName;
                string typetr = type.Substring(type.LastIndexOf('.') + 1);
                string typrs = file.ContentType;
             

                #region  查找部门名称

                Hashtable htable = new Hashtable();
            


                #endregion
             

                string year = DateTime.Now.Year.ToString();
                string month = DateTime.Now.Month.ToString();
                string filenames = year + "年" + month + "月统计表" + "." + typetr;

                file.SaveAs(uploadPath + filenames);
                //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                // string filesid = context.Request.QueryString["newsid"];
                //AfterFileSave();
                AfterFileSave(filenames);
                context.Response.Write("1");

                //上传文件完成之后

            }
            else
            {
                context.Response.Write("0");
            }
        }
        protected void AfterFileSave(string filenames)
        {
            Hashtable htable = new Hashtable();
            string FileID = Guid.NewGuid().ToString();
            string tjdate = DateTime.Now.ToString();
            string year = DateTime.Now.Year.ToString();
            string month = DateTime.Now.Month.ToString();
            string filename = filenames;
            string fileUrl = "uploadfile/" + filenames;
           
            htable.Add("FileID", FileID);
            htable.Add("tjdate", tjdate);
            htable.Add("filename", filenames);
            htable.Add("fileUrl", fileUrl);
            htable.Add("year", year);
            htable.Add("month", month);
            htable.Add("cj",1);
            SQLServerDAL.databyhtable dtable = new SQLServerDAL.databyhtable();
            dtable.databyhtables(htable, "add", "TJfile", "", "");

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}